/**   
 * Copyright 2011 COMERE Harold
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package etrl.shaders;

/**
 * \class VertexAttributeVariable
 * \brief Specialized Variable describing a
 * vertex attribute variable
 * 
 * @author harold
 * 
 */
class VertexAttributeVariable extends Variable
{
	/**
	 * \brief Initializes the vertex attribute variable
	 * 
	 * @param name
	 *            Name of the variable
	 * @param type
	 *            Type of the variable
	 * @param forwardMode
	 *            Forward mode to apply on the variable
	 */
	public VertexAttributeVariable(String name, GLSLType type, VariableForwardMode forwardMode)
	{
		super(name, type, forwardMode, true, true, 1);
	}

	/**
	 * {@inheritDoc}
	 */
	public String getDeclarationInVertexShader()
	{
		String declaration = new String("");

		switch (_forwardMode)
		{
		case VERTEX_ONLY:
			declaration += "attribute " + _type.asString() + " in_" + _name + ";\n";
			break;

		case VERTEX_AND_FRAGMENT:
			declaration += "attribute " + _type.asString() + " in_" + _name + ";\n";
			declaration += "varying " + _type.asString() + " out_" + _name + ";\n";
			break;

		default:
			System.out.println("[VertexAttributeVariable::getDeclarationInVertexShader] Unexpected error : How do you get there ??");
			break;
		}

		return declaration;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getDeclarationInFragmentShader()
	{
		String declaration = new String("");

		switch (_forwardMode)
		{
		case VERTEX_ONLY:
			// No declaration in fragment shader
			break;

		case VERTEX_AND_FRAGMENT:
			declaration += "varying " + _type.asString() + " out_" + _name + ";\n";
			break;

		default:
			System.out.println("[VertexAttributeVariable::getDeclarationInFragmentShader] Unexpected error : How do you get there ??");
			break;
		}

		return declaration;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getInitialization()
	{
		String initialization = new String("");

		switch (_forwardMode)
		{
		case VERTEX_ONLY:
			initialization += "\t" + _type.asString() + " " + _name + " = in_" + _name + ";\n";
			break;

		case VERTEX_AND_FRAGMENT:
			initialization += "\tout_" + _name + " = in_" + _name + ";\n";
			break;

		default:
			System.out.println("[VertexAttributeVariable::getInitialization] Unexpected error : How do you get there ??");
			break;
		}

		return initialization;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getBindingId()
	{
		return new String("in_") + _name;
	}

	/**
	 * {@inheritDoc}
	 */
	public String nameWithPrefix(ShaderStage stage)
	{
		String nameWithPrefix = new String("");

		switch (stage)
		{
		case VERTEX:

			switch (_forwardMode)
			{
			case VERTEX_ONLY:
				nameWithPrefix = _name;
				break;

			case VERTEX_AND_FRAGMENT:
				nameWithPrefix = new String("out_") + _name;
				break;

			default:
				System.out.println("[VertexAttributeVariable::nameWithPrefix] Unexpected error (1) : How do you get there ??");
				break;
			}

			break;

		case FRAGMENT:

			switch (_forwardMode)
			{
			case VERTEX_AND_FRAGMENT:
				nameWithPrefix = new String("out_") + _name;
				break;

			default:
				System.out.println("[VertexAttributeVariable::nameWithPrefix] Unexpected error (2) : How do you get there ??");
				break;
			}

			break;
		}

		return nameWithPrefix;
	}
}
